﻿using DotNet4.Utilities.UtilReg;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 大话西游子线程控制补丁
{
	public class ThreadList
	{
		private SortedList<int,Thread> list;
		/// <summary>
		/// 刷新所有可能的进程
		/// 如果原来存在进程，则跳过，否则创建
		/// 从ID=1开始，到创建失败结束
		/// </summary>
		public int Refresh(out int hdlThread)
		{
			int result=0, notBuildNum=0;
			list = new SortedList<int,Thread>();
			int index = 1;
			do
			{
				index++;
				if (list.Count >= index  && list[index].IsValue)
				{
					if (list[index].Refresh()>0)
					{
						result++;
						list[index] = null;
					}
					continue;
				}//如果进程存在且有效则不创建
				var thread = new Thread("C" + index);
				if (!thread.IsValue)//如果此时就是无效的，说明此进程从来没有创建过
				{
					if(notBuildNum++>10)break;
				}
				else
				{
					if (thread.Refresh()>0) {
						result++;
						continue;
					}//如果此进程该死,就不管他了
					list[index]=thread;
				}
				
			} while (1 == 1);
			hdlThread = index- notBuildNum;
			return result;
		}
	}
	public class Thread:RegInfo
	{
		private bool isValue;
		private int lastRun;
		private int beginRun;
		public Thread(string index):base(index)
		{
			if (this.Pid.Length > 0) 
			{
				string runtime = this.Runtime;
				if (runtime.Length > 0)
				{
					lastRun = Convert.ToInt32(runtime);
					beginRun = lastRun;
				}

				isValue = true;
			}

		}

		public bool IsValue { get => isValue;private set => isValue = value; }
		/// <summary>
		/// 刷新此进程的状态
		/// </summary>
		/// <returns>本次刷新后关闭了此进程</returns>
		public int Refresh()
		{
			string tmpRun = this.Runtime;
			int thisRun;
			try
			{
				 thisRun = Convert.ToInt32(this.Runtime);
			}
			catch (Exception)
			{
				return 0;
			}

			//if (thisRun == lastRun)
			//{
			//	IsValue = false;
			//	return -1;
			//}
			lastRun = thisRun;
			if ((lastRun - beginRun)/1000 > Chk_CloseTime)
			{
				return this.Close(null, "超过" + Chk_CloseTime + "秒");
			}
			else
			{
				return this.Close(Chk_Close,"含有关键字");
			}
		}
	}
	public class RegInfo
	{
		protected static string Cmd_Close = "#subClose#";
		private static string chk_CloseStatus = "失败";
		private static int chk_CloseTime = 300;
		protected static Reg RegCmd;
		private string threadID;
		private string pid;
		private string runtime;
		private string status;
		static RegInfo()
		{
			RegCmd = new Reg().In("sfMinerDigger").In("Main").In("Thread");
		}
		public RegInfo(string threadID)
		{
			this.threadID = threadID;
		}

		public string Pid { get { return RegCmd.In(threadID).GetInfo("corePid"); } private set => pid = value; }
		public string Runtime { get { return RegCmd.In(threadID).GetInfo("Runtime"); } private set => runtime = value; }
		public string Status { get { return RegCmd.In(threadID).GetInfo("Status"); } private set => status = value; }

		public static string Chk_Close { get => chk_CloseStatus; set => chk_CloseStatus = value; }
		public static int Chk_CloseTime { get => chk_CloseTime; set => chk_CloseTime = value; }

		public void SetCmd(string cmd,string key= "Task")
		{
			RegCmd.In(threadID).SetInfo(key, cmd);
		}
		protected int Close(string checkStatus=null,string reason= "")
		{
			if (checkStatus != null)
			{
				if (this.Status.IndexOf(checkStatus) <=0) return 0;
			}
			this.SetCmd("强制关闭"+ (reason.Length>0?":"+ reason : ""), "Status");
			this.SetCmd(Cmd_Close);
			this.SetCmd("1", "Refreshed");
			return 1;
		}
	}
}
